package com.kinon.ycjshop.tools;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * Created by luohao on 2017-07-21.
 */

public class SPManage {
    /**
     * sharedprefenrences的文件名
     */
    private static final String SP_NAME = "marke_user";
    private static SharedPreferences sp = null;
    private static SPManage spManage = null;
    private static Editor editor = null;
    private static Context mContext = null;

    private SPManage(Context context) {

        sp = context.getSharedPreferences(SP_NAME,
                Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    public static SPManage getInstance(Context context) {
        mContext = context;
        if (spManage == null || sp == null || editor == null) {
            spManage = new SPManage(mContext);
        }
        return spManage;
    }

    public void spput(String key, String value) {
        editor.putString(key, value);
    }

    /**
     * 获得sp中的String数据
     */
    public String getString(String key) {
        return sp.getString(key, "");
    }

    public boolean getBoolen(String key) {
        return sp.getBoolean(key, false);
    }

    public String getToken() {
        return sp.getString("token", "");
    }

    public void putCommit(String key, String value) {
        editor.putString(key, value);
        commit();
    }

    public void putBCommit(String key, boolean value) {
        editor.putBoolean(key, value);
        commit();
    }

    public void putICommit(String key, int value) {
        editor.putInt(key, value);
        commit();
    }


    public void commit() {
        editor.commit();
    }

    /**
     * 清空sp里面的内容
     */
    public void removesp() {
        editor.clear();
        editor.commit();
    }

    /**
     * 删除sp里面的特定字段
     */
    public void removeKey(String key) {
        editor.remove(key);
        editor.commit();
    }


}
